$(function (){
	//删除加载图
	$("#sg_cmt_loading").remove();
	
	//js初始化样式
	$(".wx_preview_box").height($(window).height());

	//检测音乐dom有的话就加入,防止文章库插音乐没有音乐响
	var music = $('#media_tym').html();
    if(typeof(music) == 'undefined'){
        $('#middle_content').append("<audio loop='' src='' id='media_tym' autoplay='' preload=''></audio>");
    }
    //end
    
	if(article_source){
	       
		switch(article_source){
			case 'wx':
				wx_article();
			break;
			case 'toutiao':
				toutiao_article();
			break;
			case 'xwqq':
				xwqq_article();
			break;
            case 'ifeng':
                ifeng_article();
            break;
		}
	}

	//头部标题导航点击保存
	$(".article_save").click(function(){
		$(".wx_preview_box").show();
		//初始化分享图片
	    sharePicture();
	});

	//继续编辑
	$(".wx_preview_share_cencel span").click(function(){
		$(".wx_preview_box").hide();
	});

	//点击修改分享按钮
	$(".edit_search_content_btn").click(function(){
		$(".edit_search_content").show();
		$(".wx_preview_box").hide();
		$(".tedit_header").hide();
		$("#middle_content").hide();
	    //初始化分享图片
	    sharePicture();
	});



});

//清除图片
function clear_picture(obj){
	$('#upload').val('');
	$(obj).parent().parent().find('img').attr('src','');
}

//保存文章
function success_post(){
	//文章内容
	var content = $('#middle_content').html();
	//音乐主键id
	var music_id = $('#form_music_id').val();
	//微信分享图片
	var upload = $('#upload').val();
	//文章id
	var article_id = $('#aid').val();
	//微信分享描述
	var share_introduction = $('#share_introduction').val();
	var type = $("#article_type").val();
	var title = $('#new_title').val();

	if(title == ''){
		$('#banner').show();
		layer.open({
			content:'文章标题不能为空'
		});
		return false;
	}

	$.ajax({
		type: "POST",
		url: article_submit,
		data: "content="+content+"&title="+title+"&type="+type+"&music_id="+music_id+"&share_introduction="+share_introduction+"&upload="+upload+"&jumpurl="+article_submit_success_jump_url+"&article_type_task="+article_type_task+"&aid="+article_id,
		success: function(msg){
			if(msg.status == '1'){
				//用于弹出文章审核提示
				if(msg.open == '1'){
					layer.open({
						content:msg.msg,
						end:function(){
							window.location.href = msg.url;
						}
					});
				}else{
					window.location.href = msg.url;
				}
	               
			}else{
				layer.open({
					content:msg.msg
				});
			}
		}
	});
}




/*
	获取微信文章内容后处理图片上传事件或视频等事件
 */
function wx_article(){
	var length = $('img').length;
		
	//替换图片
	if(length){
		for(var a = 0;a< length;a++){
			var src  = $('img').eq(a).attr('data-src');
			var type = $('img').eq(a).attr('data-type'); //获取微信图片类型
           
			if(src){
				
				//匹配链接中是否有gif，有就gif类型
				var judge = src.search(/=gif/);
				if(judge != '-1'){
					type = 'gif';
				}
			
				//传递url
				$.ajax({
				   type: "POST",
				   url: file_picture,
				   data: "url="+src+"&type="+type,
				   async:false,
				   success: function(msg){
					 $('img').eq(a).attr('src',images_url+msg.msg);
				   }
				});
			}
		}
	};
	var iflength = $('iframe').length;
	if(iflength){
		for(var a = 0;a< length;a++){
			var src = $('iframe').eq(a).attr('data-src');

			if($('iframe').hasClass('js_editor_vote_card')){
				//微信投票系统删除
				$('iframe.js_editor_vote_card').remove();
			}else if(typeof(src) !== 'undefined'){
				$('iframe').eq(a).attr('src',"http://mp.weixin.qq.com"+src);
			}
		}
	}
	
	//找到微信标题
	var title = $.trim($('#activity-name').text());
	//描述-文章内容20个字
	var desc = $.trim($('.rich_media_content').text().replace(/<\/?.+?>/g,""));
	desc = desc.substr(0, 38);
	initViewtitleDesc(title,desc);

	//微信投票隐藏
	$('#middle_content .vote_area').hide();
}

function toutiao_article(){
	//找到标题
	var title = $.trim($('h1.title').text());
	//描述-文章内容20个字
	var desc = $.trim($('.article-content').text().replace(/<\/?.+?>/g,""));
	desc = desc.substr(0, 38);
	initViewtitleDesc(title,desc);
}

//xw.qq.com
function xwqq_article(){
	//找到标题
	var title = $.trim($('h1.title').text());
	//描述-文章内容20个字
	var desc = $.trim($('#middle_content .content').text().replace(/<\/?.+?>/g,""));	
	desc = desc.substr(0, 38);
	initViewtitleDesc(title,desc);
}

//凤凰网
function ifeng_article(){
    //找到标题
	var title = $.trim($('.acTxt .acTxtTit h1').text());
    for(var a =0;a<$('.acTxt img').length;a++){
        var img = $('.acTxt img').eq(a).attr('data-original');
        if(img != '' || img != undefined){
			$('.acTxt img').eq(a).attr('src',img);
        }        
    }
	initViewtitleDesc(title,title);
}


function initViewtitleDesc(title,desc){
	//分享的标题
	$('#new_title').val(title);
	//分享描述
	$("#share_introduction").val(desc);

	//分享预览标题
	$('.wx_preview_share_view_circle_content_text,.wx_preview_share_view_friend_title').html(title);
	//分享预览描述
	$(".wx_preview_share_view_friend_desc").html(desc);
}